local qimou = fk.CreateSkill {
  name = "lvshi__qimou",
  tags = {"Phase"},
}

Fk:loadTranslationTable{
  ["lvshi__qimou"] = "奇谋",
  [":lvshi__qimou"] = "阶段技，你可选择一项：1.和一名己方角色交换体力值；2.失去体力至1点，此回合增加失去体力等量的攻击范围。",
  ["#lvshi__qimou"] = "请选择一项，若选择交换体力值则选择目标角色",
  ["lvshi__qimou1"] = "和一名己方角色交换体力值",
  ["lvshi__qimou2"] = "失去体力至1点",
  ["@lvshi__qimou-turn"] = "奇谋",

  ["$lvshi__qimou1"] = "兵行死地方建功，敢笑诸葛不丈夫！",
  ["$lvshi__qimou2"] = "战机稍纵即逝，怎可犹豫不前！",
}
local ls = require "packages/lvshi/utility"

qimou:addEffect("active", {
  anim_type = "offensive",
  prompt = "#lvshi__qimou",
  card_num = 0,
  max_target_num = 1,
  max_phase_use_time = 1,
  interaction = function(self, player)
    local all_choices = {"lvshi__qimou1", "lvshi__qimou2"}
    local choices = table.simpleClone(all_choices)
    if player.hp <= 1 then table.removeOne(choices, "lvshi__qimou2") end
    return UI.ComboBox { choices = choices, all_choices = all_choices }
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected, selected_cards)
    if self.interaction.data == "lvshi__qimou1" then
      return #selected == 0 and to_select ~= player and ls.isFriend(player, to_select)
    elseif self.interaction.data == "lvshi__qimou2" then
      return false
    end
  end,
  feasible = function(self, player, selected, selected_cards)
    if self.interaction.data == "lvshi__qimou1" then
      return #selected == 1
    elseif self.interaction.data == "lvshi__qimou2" then
      return #selected == 0
    end
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    if self.interaction.data == "lvshi__qimou2" then
      if player.hp > 1 then
        local n = player.hp - 1
        room:loseHp(player, n, qimou.name)
        room:addPlayerMark(player, "@lvshi__qimou-turn", n)
      end
    else
      local target = effect.tos[1]
      if player.hp ~= target.hp then
        local n = math.min(player.hp, target.maxHp)
        local m = math.min(player.maxHp, target.hp)
        room:setPlayerProperty(player, "hp", m)
        room:setPlayerProperty(target, "hp", n)
      end
    end
  end,
})

qimou:addEffect("atkrange", {
  correct_func = function (self, from, to)
    if from:getMark("@lvshi__qimou-turn") > 0 then
      return from:getMark("@lvshi__qimou-turn")
    end
  end,
})

return qimou
